package com.vincent.kdp.Activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.google.gson.Gson;
import com.vincent.baseapp.util.SPUtil;
import com.vincent.kdp.PostUrl;
import com.vincent.kdp.R;
import com.vincent.kdp.bean.BasEmployeeVO;
import com.vincent.kdp.bean.LoginResult;
import com.vincent.kdp.ui.other.MainActivity;
import com.vincent.kdp.util.BaseParam;
import com.vincent.kdp.util.GsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class LoginActivity extends Activity{

    @Bind(R.id.back_img)
    ImageView mBackImg;
    @Bind(R.id.toolbar_center_tv)
    TextView mToolbarCenterTv;
    @Bind(R.id.vip_account_et)
    EditText username;
    @Bind(R.id.password)
    EditText password;
    @Bind(R.id.login_tv)
    TextView login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        mToolbarCenterTv.setText("登录");
    }

    @OnClick(R.id.back_img)
    public void back() {
        MainActivity.instance.setTabSelection(1);
        finish();
        this.finish();
        overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
    }

    @OnClick(R.id.login_tv)
    public void onClick() {
        //登录请求
        if(username.getText().toString().isEmpty() &&
                password.getText().toString().isEmpty()){
            Toast.makeText(LoginActivity.this,"请输入用户名或密码",Toast.LENGTH_SHORT).show();

        }else {
            RequestParams params = new RequestParams (PostUrl.WORKER_LOGIN);
            params.addBodyParameter("number",BaseParam.getDeviceId());
            params.addBodyParameter("userName", username.getText().toString());
            params.addBodyParameter("password", password.getText().toString());
            params.addBodyParameter("clientid", SPUtil.getString("clientid"));
            x.http().post(params,new Callback.CommonCallback<String>(){
                public String name2;
                @Override
                public void onSuccess(String result) {

                    Gson gson= GsonUtils.build();
                    LoginResult loginResult=gson.fromJson(result, LoginResult.class);
                    if(loginResult.isSuccess()){
                        BasEmployeeVO employeeVO=loginResult.getData();
                        SPUtil.put("name",employeeVO.getName());
                        SPUtil.put("username",employeeVO.getUserName());
                        SPUtil.put("empId",employeeVO.getId()+"");
                        MainActivity.instance.setTabSelection(3);
                        finish();
                    }else{
                        Toast.makeText(LoginActivity.this,loginResult.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                }
                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    ex.printStackTrace();
                    Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                }
                @Override
                public void onCancelled(CancelledException cex) {
                }

                @Override
                public void onFinished() {
                }
            });
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            MainActivity.instance.setTabSelection(1);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
